package com.example.stock.service.impl;

import com.example.stock.comm.FreqEnum;
import com.example.stock.comm.StrategyEnum;
import com.example.stock.entity.eo.StockStatEo;
import com.example.stock.entity.service.IStockStatService;
import com.example.stock.service.IServiceStatistics;
import com.example.stock.util.StockStatUtil;
import com.example.stock.vo.DataVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class ServiceStatisticsImpl implements IServiceStatistics {

    @Resource
    private IStockStatService IStockStatService;
    @Resource
    private StockStatUtil stockStatUtil;

    public void statisticsEmaUp(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 10; i < 100; i++) {
            for (int j = 20; j < 100; j++) {
                int m = j * 1;
                int n = i * 1;
                Double finalMoney = IStockStatService.analysisEmaUp(m, n, closeData, false, tsCode, freq);
                stockStatUtil.recordAll(m, n, finalMoney, null, tsCode, strategy);
                System.out.println("m: " + m + "n: " + n);
            }
        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo =  IStockStatService.getTopOne(tsCode,strategy);
        IStockStatService.analysisEmaUp(stockStatEo.getM(), stockStatEo.getN(), closeData, true, tsCode, freq);
    }

    public void statisticsEmaRef0(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 1; i < 100; i++) {
            for (int j = 1; j < 100; j++) {
                int m = j * 1;
                int n = i * 1;
                Double finalMoney = IStockStatService.analysisEmaRef0(m, n, closeData, false, tsCode, freq);
                stockStatUtil.recordAll(m, n, finalMoney, null, tsCode, strategy);
                System.out.println("m: " + m + "n: " + n);
            }
        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo = IStockStatService.getTopOne(tsCode,strategy);
        IStockStatService.analysisEmaRef0(stockStatEo.getM(), stockStatEo.getN(), closeData, true, tsCode, freq);

    }

    public void statisticsMaUp(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 1; i < 1000; i++) {
            Double finalMoney = IStockStatService.analysisMaUp(i, closeData, false, tsCode, freq);
            stockStatUtil.recordAll(i, 0, finalMoney, null, tsCode, strategy);
            System.out.println("m: " + i);

        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo =  IStockStatService.getTopOne(tsCode,strategy);
        IStockStatService.analysisMaUp(stockStatEo.getM(),  closeData, true, tsCode, freq);
    }

    public void statisticsMaCross(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 1; i < 100; i++) {
            for (int j = 1; j < 100; j++) {
                int m = j * 1;
                int n = i * 1;
                Double finalMoney = IStockStatService.analysisMaCross(m, n, closeData, false, tsCode, freq);
                stockStatUtil.recordAll(m, n, finalMoney,null, tsCode, strategy);
                System.out.println("m: " + m + "n: " + n);
            }
        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo =  IStockStatService.getTopOne(tsCode,strategy);
        IStockStatService.analysisMaCross(stockStatEo.getM(), stockStatEo.getN(), closeData, true, tsCode, freq);
    }

    public void statisticsMaPure(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 1; i < 5000; i++) {
                int m = i * 1;
                Double finalMoney = IStockStatService.analysisMaPure(m, closeData, false, tsCode, freq);
                stockStatUtil.recordAll(m, 0, finalMoney,null, tsCode, strategy);
                System.out.println("m: " + m );
        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo =  IStockStatService.getTopOne(tsCode,strategy);
        IStockStatService.analysisMaPure(stockStatEo.getM(), closeData, true, tsCode, freq);
    }

    private void statisticsEmaUpComplex(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategy) {
        IStockStatService.delete(tsCode, strategy);
        for (int i = 1; i < 25; i++) {
            for (int j = 1; j < 60; j++) {
                int m = j * 1;
                int n = i * 1;
                Double finalMoney = IStockStatService.analysisEmaUpComplex(m, n, closeData, false, tsCode, freq);
                stockStatUtil.recordAll(m, n, finalMoney, null, tsCode, strategy);
                System.out.println("m: " + m + "n: " + n);
            }
        }
        stockStatUtil.finishRecordAll();
        StockStatEo stockStatEo = IStockStatService.getTopOne(tsCode, strategy);
        IStockStatService.analysisEmaUpComplex(stockStatEo.getM(), stockStatEo.getN(), closeData, true, tsCode, freq);
    }

    public void statistics(List<DataVo> closeData, String tsCode, FreqEnum freq, StrategyEnum strategyEnum) {
        switch (strategyEnum) {
            case EMA_UP:
                statisticsEmaUp(closeData, tsCode, freq, strategyEnum);
                break;
            case MA_UP:
                statisticsMaUp(closeData, tsCode, freq, strategyEnum);
                break;
            case EMA_REF0:
                statisticsEmaRef0(closeData, tsCode, freq, strategyEnum);
                break;
            case MA_CROSS:
                statisticsMaCross(closeData, tsCode, freq, strategyEnum);
                break;
            case MA_PURE:
                statisticsMaPure(closeData, tsCode, freq, strategyEnum);
            case EMA_UP_COMPLEX:
                statisticsEmaUpComplex(closeData, tsCode, freq, strategyEnum);
                break;
            default:
                throw new RuntimeException("无法识别的枚举 111");

        }

    }


}
